package com.modern.js.controller;


import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.modern.js.entity.Employee;
import com.modern.js.entity.Orders;
import com.modern.js.pojo.OrdersAssist;
import com.modern.js.service.IOrdersService;
import com.modern.js.utils.StringUtils;
import org.apache.ibatis.annotations.Param;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpSession;
import java.util.HashMap;
import java.util.Map;

/**
 * <p>
 *  前端控制器
 * </p>
 *
 * @author PIGS
 * @since 2020-03-28
 */
@RestController
@RequestMapping("/orders")
public class OrdersController {
    @Autowired
    private IOrdersService iOrdersService;

    @GetMapping("/list")
    public Map getList(Integer page, Integer limit,String customerName,String busubessName){
        Map<String,Object> map = new HashMap<String,Object>();
        Page<OrdersAssist> ordersAssistPage = new Page<OrdersAssist>();
        ordersAssistPage.setSize(limit);//设置每页记录数
        ordersAssistPage.setCurrent(page);//设置当前页码
        IPage<OrdersAssist> iPage = iOrdersService.selectAllOrders(ordersAssistPage,customerName,busubessName);
        if (iPage.getTotal()!=0){
            map.put("msg","查询成功！");
        }else {
            map.put("msg","查询失败！");
        }
        map.put("data",iPage.getRecords());
        map.put("count",iPage.getTotal());
        map.put("code",0);
        return map;
    }


    @GetMapping("/listById")
    public Map ListById(Integer page, Integer limit, HttpSession session){
        Map<String,Object> map = new HashMap<String,Object>();
        Employee employee = (Employee) session.getAttribute("employee");
        Integer empId = employee.getEmpId();
        Page<OrdersAssist> ordersAssistPage = new Page<OrdersAssist>();
        ordersAssistPage.setSize(limit);//设置每页记录数
        ordersAssistPage.setCurrent(page);//设置当前页码
        IPage<OrdersAssist> iPage = iOrdersService.selectAllOrdersEmpId(ordersAssistPage,empId);
        if (iPage.getTotal()!=0){
            map.put("msg","查询成功！");
        }else {
            map.put("msg","查询失败！");
        }
        map.put("data",iPage.getRecords());
        map.put("count",iPage.getTotal());
        map.put("code",0);
        return map;
    }

    @DeleteMapping("/del/{ordersId}")
    public boolean delOrder(@PathVariable Integer ordersId){
        System.out.println("你好订单");
        Orders orders = new Orders();
        orders.setOrdersId(ordersId);
        orders.setIsDel(0);
        boolean result = iOrdersService.updateById(orders);
        return result;
    }

    @PostMapping("/add")
    public boolean addOrders(Orders orders){
        orders.setIsDel(1);
        orders.setCreateTime(StringUtils.getNowTime());
        orders.setUpdateTime(StringUtils.getNowTime());
        boolean result = iOrdersService.save(orders);
        return result;
    }

    @PutMapping("/update")
    public int updateOrder(Orders orders){
        System.out.println(orders);
        orders.setUpdateTime(StringUtils.getNowTime());
//        String updateTime = StringUtils.getNowTime();
//        map.put("updateTime",updateTime);
        int result = iOrdersService.updateOrder(orders);
        return result;
    }

}
